package com.xiaoy.core.bpm.simple.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiaoy.core.bpm.engine.entity.ProcessNodeVO;
import com.xiaoy.core.bpm.simple.dao.ProcessNodeDao;
import com.xiaoy.core.bpm.simple.entity.ProcessNode;
import com.xiaoy.util.bean.BeanUtils;

@Service
public class ProcessNodeService extends IProcessNodeAdapt {
	
	@Resource
	private ProcessNodeDao processNodeDao;
	
	
	
	@Override
	@Transactional
	public void generateProcessNodesByDefineId(String defineId)throws Exception {
		
		List<ProcessNodeVO> nodeVOs = workFlow.getProcessNodesByDefineId(defineId);
		
		for (ProcessNodeVO vo:nodeVOs) {
			ProcessNode node = new ProcessNode();
			
			node.setNodeDefineId(vo.getId());
			node.setNodeDefineName(vo.getName());
			node.setDefineId(defineId);
			save(node);
		}
	}
	@Override
	public List<ProcessNode> findProcessNodesByDefineId (String defineId) {
		
		List<ProcessNode> list = processNodeDao.findBy("defineId", defineId);
		
		return list;
	}
	
	public ProcessNode getById (String id) {
		List<ProcessNode> pns = findBy("id", id);
		if (pns.size()>0) {
			return pns.get(0);
		}
		return null;
	}
	
	public List<ProcessNode> findBy (String key,Object value) {
		
		return processNodeDao.findBy(key, value);
		
	}
	
	public void generateProcessNodeByDefineId(ProcessNode node,String defineId) {
		
		
	}
	
	public boolean isGenerateProcessNodes(String defineId) {
		
		List<?> list = findProcessNodesByDefineId(defineId);
		if (list!=null&&list.size()>0) {
			return true;
		}
		return false;
	}
	
	@Transactional
	public void save (ProcessNode node) throws Exception {
		
		long id = node.getId();
		ProcessNode n = processNodeDao.get(id);
		BeanUtils.beanCopy(node,n);
		processNodeDao.save(n);
	}
	
	
	public static void main(String[] args) {
		
		ProcessNode node1 = new ProcessNode();
		node1.setName("caonima");
		node1.setNodeDefineName("caonima1");
		node1.setCreateDate(new Date());
		
		ProcessNode node2 = new ProcessNode();
		node2.setName("caonimei");
		
//		BeanMapper.copy(node2, node1);
		
		ProcessNode node3 = BeanUtils.beanCopy(node1, ProcessNode.class);
		
		System.out.println("node1-->"+node1.getName()+"----"+node1.getNodeDefineName()+"----"+node1.getCreateDate());
		System.out.println("node2-->"+node2.getName()+"----"+node2.getNodeDefineName()+"----"+node2.getCreateDate());
		System.out.println("node3-->"+node3.getName()+"----"+node3.getNodeDefineName()+"----"+node3.getCreateDate());
		
		
		BeanUtils.beanCopy(node2, node1);
		System.out.println("node1-->"+node1.getName()+"----"+node1.getNodeDefineName()+"----"+node1.getCreateDate());
		System.out.println("node2-->"+node2.getName()+"----"+node2.getNodeDefineName()+"----"+node2.getCreateDate());
		System.out.println("node3-->"+node3.getName()+"----"+node3.getNodeDefineName()+"----"+node3.getCreateDate());
		
	}
}


